<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
        :modal-class="my-modal"
        dialog-class="modal-dialog"
        @hidden 关闭窗体触发事件
    -->
    <b-modal ref="my-modal" :title="title" scrollable @hidden="onHidden" size="lg" no-close-on-backdrop>
        <div class="d-block text-center">
            <div class="form-group m-form__group row">
                <label class="col-md-2 col-form-label">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题</label>
                <div class="col-md-9">
                    <input class="form-control" v-model="lcProcessForm.title" placeholder="请输入">
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-md-2 col-form-label">产&nbsp;品&nbsp;&nbsp;线</label>
                <div class="col-md-3">
                    <select class="form-control" style="width: 150px;" v-model="lcProcessForm.lcProductId">
                        <option value="">请选择</option>
                        <option v-for="item in productList" :key="item.id" :label="item.name"
                            :value="item.id">
                        </option>
                    </select>
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-md-2 col-form-label">产&nbsp;品&nbsp;&nbsp;组</label>
                <div class="col-md-3">
                    <select class="form-control" style="width: 150px;" v-model="lcProcessForm.lcGroupId">
                        <option value="">请选择</option>
                        <option v-for="item in groupList" :key="item.id" :label="item.name"
                            :value="item.id">
                        </option>
                    </select>
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-md-2 col-form-label">模块Key</label>
                <div class="col-md-2">
                    <input class="form-control" v-model="lcProcessForm.moduleKey" placeholder="请输入">
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-lg-2 col-form-label">属性同步</label>
                <div class="col-md-2">
                    <select class="form-control" v-model="lcProcessForm.syncAttr" placeholder="请选择">
                        <option value="">请选择</option>
                        <option value="0">包含属性</option>
                        <option value="1">复制时不同步属性</option>
                    </select>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="copyProcess">
                <i class="icon-xl fas fa-save"></i> 保存
            </button>
        </template>
    </b-modal>
</template>
<script type="text/javascript">
import apiUtil from "@/core/util/apiUtil.js";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating } from "@/core/util/jehcUtil.js";
export default {
    data() {
        return {
            title: "复制一个模型",
            id: "",
            productList: [],
            groupList: [],
            lcProcessForm: {
                syncAttr: "",
                id: "",
                lcProductId: "",
                lcGroupId: "",
                lcGroupId_: "",
                title: "",
                moduleKey: "",
            }
        }
    },
    components: {
    },
    watch: {
        "lcProcessForm.lcProductId": {//监听字段变化
            handler: function (newVal, old) {
                this.lcProcessForm.lcGroupId = "";
                if (newVal == "" || newVal == undefined || newVal == null) {
                    this.groupList = [];
                } else {
                    this.initGroupList(newVal)
                }
                if (this.lcProcessForm.lcGroupId_ == "" || this.lcProcessForm.lcGroupId_ == undefined || this.lcProcessForm.lcGroupId_ == null) {

                } else {
                    this.lcProcessForm.lcGroupId = this.lcProcessForm.lcGroupId_;
                    this.lcProcessForm.lcGroupId_ = "";
                }
            }
        },
    },
    mounted() {

    },
    methods: {
        showModal(row) {
            this.restForm();
            this.$refs['my-modal'].show();
            if (null != row && undefined != row) {
                this.id = row.id;
                this.title = "复制一个模型【" + row.title + "】";
                this.getDetail(row);
            }

        },
        copyProcess(){
            if(this.lcProcessForm.title === ""){
                handleAlert("请输入标题", "warning", 3)
                return;
            }
            if(this.lcProcessForm.moduleKey === ""){
                handleAlert("请输入模块Key", "warning", 3)
                return;
            }
            if(this.lcProcessForm.syncAttr === ""){
                handleAlert("请选择同步属性", "warning", 3)
                return;
            }
            // 保存前提示
            this.$confirm("确定拷贝该数据?", "提示", {type: "warning",}).then(() => {
                apiUtil.post(process.env.VUE_APP_BPM_API+"/lcProcess/copy", this.lcProcessForm).then(({ data }) => {
                    if(data.success){
                        handleAlert("拷贝成功", "success", 3);
                        this.hideModal();//关闭窗体
                        this.$emit("change",data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                    }else {
                        handleAlert("拷贝失败", "error", 3);
                    }            
                });
            }).catch(()=>{});//注意这里，这里是重点！！！;   
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        restForm(){
            //重置
            Object.assign(this.$data.lcProcessForm, this.$options.data().lcProcessForm);
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        onHidden() {//关闭窗体触发事件
            this.restForm();
            this.$emit("change", null);
        },
        initProductList() {
            let params = {};
            apiUtil.post(process.env.VUE_APP_BPM_API + "/lcProduct/find", params).then(({ data }) => {
                this.productList = data.data;
            });
        },
        initGroupList(id) {
            let params = {};
            apiUtil.query(process.env.VUE_APP_BPM_API + "/lcGroup/find/" + id, params).then(({ data }) => {
                this.groupList = data.data;
            });
        },
        getDetail(row) {
            apiUtil.get(process.env.VUE_APP_BPM_API + "/lcProcess/get/" + row.id).then(({ data }) => {
                this.lcProcessForm = data.data;
                this.lcProcessForm.title = this.lcProcessForm.title+"（复制）"
                this.lcProcessForm.moduleKey = this.lcProcessForm.moduleKey+"-"
                this.lcProcessForm.syncAttr = ""
                this.initProductList();
                this.initGroupList(this.lcProcessForm.lcProductId);
                this.lcProcessForm.lcGroupId_ = data.data.lcGroupId;
            });
        },
    }
}
</script>
